/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */
package acid.spring.jdbc.repositories;

import acid.spring.db.Experiment;
import acid.spring.db.ExperimentAnalysis;
import acid.spring.db.GeneAlias;
import acid.spring.db.GeneExpressionResult;
import acid.spring.db.Organism;
import acid.spring.db.Tissue;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 *
 * @author morgan
 */
@Repository
public class JdbcGeneExpressionResultRepository implements GeneExpressionResultRepository, InitializingBean
{

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Value("${jdbc.sql.geneexpressionresults.queryByGeneAliasId}")
    private String queryExpressionResultsByGeneAlias;
    @Value("${jdbc.sql.geneexpressionresults.queryByTissueId}")
    private String queryExpressionResultsByTissue;
    @Value("${jdbc.sql.geneexpressionresults.queryByExperimentId}")
    private String queryExpressionResultsByExperiment;
    
    @Override
    public void afterPropertiesSet() throws Exception
    {
        Assert.notNull(this.jdbcTemplate, "the jdbcTemplate can't be null!");
    }
    private RowMapper<GeneExpressionResult> geneExpressionResultRowMapper = new RowMapper<GeneExpressionResult>()
    {

        @Override
        public GeneExpressionResult mapRow(ResultSet resultSet, int i) throws SQLException
        {
            String blank = "This space intentionally left blank";
            //long id = resultSet.getInt("id");
            int id = resultSet.getInt("gene_expression_result_id");
            GeneAlias geneAlias = new GeneAlias(resultSet.getInt("gene_alias_id"), blank, null);
            Organism organism = new Organism(resultSet.getInt("organism_id"), blank, blank);
            Tissue tissue = new Tissue(resultSet.getInt("tissue_id"), blank);
            double foldChange = resultSet.getDouble("fold_change");
            double pVal = resultSet.getDouble("p_value");
            ExperimentAnalysis experimentAnalysis = new ExperimentAnalysis(
                    resultSet.getInt("experiment_analysis_id"), null, blank, blank,
                    blank);
            return new GeneExpressionResult(id, geneAlias, organism, tissue,
                    foldChange, pVal, experimentAnalysis);
        }
    };

    @Override
    public int saveGeneExpressionResult(GeneExpressionResult geneExpressionResult)
    {
        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
        simpleJdbcInsert.setTableName("gene_expression_result");
        simpleJdbcInsert.setColumnNames(Arrays.asList("gene_alias_id",
                "experiment_analysis_id", "organism_id",
                "tissue_id", "fold_change", "p_value"));
        simpleJdbcInsert.setGeneratedKeyName("gene_expression_result_id");

        Map<String, Object> args = new HashMap<String, Object>();
        args.put("gene_alias_id", geneExpressionResult.getGeneAlias().getGene_alias_id());
        args.put("experiment_analysis_id", geneExpressionResult.getExperimentAnalysis().getAnalysis_id());
        args.put("organism_id", geneExpressionResult.getOrganism().getOrganism_id());
        args.put("tissue_id", geneExpressionResult.getTissue().getTissue_id());
        args.put("fold_change", geneExpressionResult.getFold_change());
        args.put("p_value", geneExpressionResult.getP_value());
        Number id = simpleJdbcInsert.execute(args);
        return id.intValue();
    }

    @Override
    public List<GeneExpressionResult> queryByGeneAlias(GeneAlias geneAlias)
    {
        return jdbcTemplate.query(queryExpressionResultsByGeneAlias, 
                geneExpressionResultRowMapper, geneAlias.getGene_alias_id());
    }

    @Override
    public List<GeneExpressionResult> queryByTissue(GeneAlias geneAlias, Tissue tissue)
    {
        return jdbcTemplate.query(queryExpressionResultsByTissue, 
                geneExpressionResultRowMapper, geneAlias.getGene_alias_id(),
                tissue.getTissue_id());
    }
    @Override
    public List<GeneExpressionResult> queryByExperiment(GeneAlias geneAlias, Experiment experiment)
    {
        return jdbcTemplate.query(queryExpressionResultsByExperiment,
                geneExpressionResultRowMapper, geneAlias.getGene_alias_id(),
                experiment.getExperiment_id());
    }
}
